package test.demo;

import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson.JSON;
import tech.waterism.modelbase.PredictResult;

import java.util.Map;

/**
 * SMS_3 和 LAG_3 串联运行
 * SMS_3 的产流输出作为 LAG_3 的汇流输入
 *
 * @author AI Assistant
 * @version 1.0
 */
public class SMS3_LAG3_Pipeline {

    public static void main(String[] args) {
        System.out.println("========== 开始 SMS_3 产流模型 ==========");

        // 1. 读取 SMS_3 输入文件并运行产流模型
        String sms3Input = ResourceUtil.readUtf8Str("20810200-方案178-SMS_3-1762739718330-20250605080000.txt");
        Map<String, String> sms3Data = JSON.parseObject(sms3Input, Map.class);
        SMS_3 sms3Model = new SMS_3(sms3Data);
        PredictResult sms3Result = sms3Model.predict();

        System.out.println("SMS_3 产流结果:");
        System.out.println("  - 总产流 (RunoffSim): " + (sms3Result.getRunoffSim() != null ? sms3Result.getRunoffSim().length + " 个时段" : "null"));
        System.out.println("  - 分项产流 (RSim): " + (sms3Result.getRSim() != null ? sms3Result.getRSim().length + " 种产流类型" : "null"));
        if (sms3Result.getRSim() != null) {
            System.out.println("    - 地表产流 RS: " + sms3Result.getRSim()[0].length + " 个值");
            System.out.println("    - 土壤产流 RI: " + sms3Result.getRSim()[1].length + " 个值");
            System.out.println("    - 地下产流 RG: " + sms3Result.getRSim()[2].length + " 个值");
        }

        System.out.println("\n========== 开始 LAG_3 汇流模型 ==========");

        // 2. 读取 LAG_3 输入文件
        String lag3Input = ResourceUtil.readUtf8Str("20810200-方案178-LAG_3-1762739718359-20250605080000.txt");
        Map<String, String> lag3Data = JSON.parseObject(lag3Input, Map.class);

        // 3. 将 SMS_3 的 RSim 输出转换为 LAG_3 的 rSim 输入
        double[][] rSim = sms3Result.getRSim();
        String rSimJson = JSON.toJSONString(rSim);
        lag3Data.put("rSim", rSimJson);

        System.out.println("已将 SMS_3 的产流结果注入 LAG_3 的 rSim 参数");

        // 4. 运行 LAG_3 汇流模型
        LAG_3 lag3Model = new LAG_3(lag3Data);
        PredictResult lag3Result = lag3Model.predict();

        System.out.println("\nLAG_3 汇流结果:");
        System.out.println("  - 流量过程 (QSim): " + (lag3Result.getQSim() != null ? lag3Result.getQSim().length + " 个时段" : "null"));
        if (lag3Result.getQSim() != null && lag3Result.getQSim().length > 0) {
            System.out.println("  - 前5个流量值:");
            for (int i = 0; i < Math.min(5, lag3Result.getQSim().length); i++) {
                System.out.printf("    时段 %d: %.3f m³/s%n", i + 1, lag3Result.getQSim()[i]);
            }
        }

        System.out.println("\n========== 串联运行完成 ==========");
        System.out.println("\n完整结果:");
        System.out.println("SMS_3 结果: " + sms3Result);
        System.out.println("\nLAG_3 结果: " + lag3Result);
    }
}

